/**
 * Copyright (C) 2010 Hamburg Sud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamburgsud.log4testing.core.data;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.ReadablePeriod;

/**
 * Every test object even if it is a simple test step or a complex test suite
 * with several test cases and test suites as its children must provide the
 * general information of this object.</br> A test object of log4testing is an
 * object which stores logging information about the execution of test commands.
 * Test commands can be technical or more domain based ones what depends on the
 * context in which log4testing is used.
 */
abstract public class TestObject implements Path {

	/**
	 * Some explanations about the test object and what it should do. The text
	 * should be human readable and written in a way which can be understood by
	 * people who will read test results.
	 */
	private String comment = null;
	
	private String path;
	private String pathToBaseDir;

	/**
	 * Each TestObject has a unique id. On the next creation of a TestObject the
	 * next TestObject gets the id of this attribute. For obtaining a new id, use
	 * {@link #getNextId()}.
	 */
	private static int nextId = 0;

	/**
	 * Unique id of an TestObject. This id is unique in the context of the virtual
	 * machine in which this code is running. The id is not unique across
	 * several java virtual machine instances and different executions.
	 */
	private String id = getNextId();	
	
	// TODO Should be checked in a JUnit testcase.
	// Creation of multiple threads which try to create new test steps.
	// When this method wouldn't be synchronized, some test objects will have
	// the same id.
	/**
	 * Returns the next unique id which is used for the identification of this
	 * {@link TestObject}.
	 * 
	 * @return the next unique id
	 * @see TestObject#id
	 */
	synchronized private static String getNextId() {
		int id = nextId;
		++nextId;
		return Integer.toString(id);
	}
	
	/**
	 * @return the unique id of this test object
	 */
	public String getId() {
		return id;
	}
	
	/**
	 * @return time when the test object has been started
	 */
	abstract public DateTime getStartingTime();

	/**
	 * @return time when the test object has been finished
	 */
	abstract public DateTime getFinishingTime();

	// TODO the following function should be replaced with is(TestStatus.OK) or
	// is(TestStatus.ERROR)
	// Before changing this it is to be checked how the method would be used in
	// Velocity.
	/**
	 * @return <code>true</code> if a test represented by this test object has
	 *         failed; <code>false</code> otherwise
	 */
	abstract public boolean isFailed();

	/**
	 * Calculates and returns the duration of how long it took to execute the
	 * test operations whose test results are represented by this test object
	 * 
	 * @return duration duration between starting and finishing time
	 */
	public ReadablePeriod getDuration() {
		final DateTime startingTime = getStartingTime();
		final DateTime finishingTime = getFinishingTime();
		ReadablePeriod duration = new Period();
		if (startingTime != null && finishingTime != null) {
			duration = new Interval(startingTime, finishingTime).toPeriod();
		}
		return duration;
	}

	/**
	 * Checks if the test object is finished. It is finished when the finishing
	 * time has been set.
	 * 
	 * @return <code>true</code> if it is finished; <code>false</code> otherwise
	 */

	public boolean isFinished() {
		return getFinishingTime() != null;
	}

	/**
	 * Is used to set a comment which explains what this test object shall do. A
	 * test writer can provide information like a textual explanation are some
	 * details about settings, parameters or other data used for test actions
	 * represented by this test object.
	 * 
	 * @param comment
	 *            with some detailed information about the test object
	 * @return the test object itself
	 */
	public TestObject setComment(final String comment) {
		this.comment = comment;
		return this;
	}

	/**
	 * @return the comment with some detailed information about the test object
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * Clears the comment by setting it to <code>null</code>.
	 */
	public TestObject clearComment() {
		comment = null;
		return this;
	}
	
	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(final String path) {
		this.path = path;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}
	

	public void setPathtobasedir(final String path) {
		this.pathToBaseDir = path;
	}
	
	public String getPathtobasedir() {
		return pathToBaseDir;
	}

}
